﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using YArchitech.Revit.RevitExtension;
using YJKArchMethodLibrary.Common;
using YJKArchMethodLibrary.Utils;
using YRevitJig;

namespace Footstep.CreateFootstep
{
	public class FootstepDesigner
	{
		public FootstepDesigner(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.GetInitializeInfo();
			this.m_uiDoc = cmdData.Application.ActiveUIDocument;
			this.m_drawJigCurve = new DrawJigCurve(cmdData);
		}

		public List<Level> Levels
		{
			get
			{
				List<Level> list = new List<Level>();
				foreach (ElementId elementId in this.m_LevelIds)
				{
					Element element = this.m_Revit.Application.ActiveUIDocument.Document.GetElement(elementId);
					Level level = element as Level;
					if (level != null)
					{
						list.Add(level);
					}
				}
				return list;
			}
		}

		public List<double> LevelsElevation
		{
			get
			{
				List<double> list = new List<double>();
				foreach (ElementId elementId in this.m_LevelIds)
				{
					Element element = this.m_Revit.Application.ActiveUIDocument.Document.GetElement(elementId);
					Level level = element as Level;
					list.Add(AssistFunc.feetToMM(level.Elevation) / 1000.0);
				}
				return list;
			}
		}

		private void GetInitializeInfo()
		{
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			List<Level> sortLevels = Common.GetSortLevels(document, double.MinValue, double.MaxValue);
			foreach (Level level in sortLevels)
			{
				if (level != null)
				{
					this.m_LevelIds.Add(level.Id);
				}
			}
		}

		public void Design()
		{
			List<Material> allMaterial = Common.GetAllMaterial(this.m_Revit.Application.ActiveUIDocument.Document);
			CreateFootstepForm createFootstepForm = new CreateFootstepForm(this.Levels, this.LevelsElevation, allMaterial, this.m_Revit);
			while (createFootstepForm.ShowDialog() == DialogResult.OK)
			{
				try
				{
					string text = "";
					bool flag = false;
					switch (createFootstepForm.theResultType)
					{
					case CreateFootstepForm.ResultType.RT_Rect1:
						flag = this.CreateRectOneSideFootstep(createFootstepForm.BottomLevel, createFootstepForm.Offset, createFootstepForm.IsInputPlatformWidth, createFootstepForm.PlatformWidth, createFootstepForm.StepCount, createFootstepForm.StepWidth, createFootstepForm.StepHeight, createFootstepForm.FootMaterial, ref text);
						break;
					case CreateFootstepForm.ResultType.RT_Rect2:
						flag = this.CreateRectSecondSideFootstep(createFootstepForm.BottomLevel, createFootstepForm.Offset, createFootstepForm.IsInputPlatformWidth, createFootstepForm.PlatformWidth, createFootstepForm.StepCount, createFootstepForm.StepWidth, createFootstepForm.StepHeight, createFootstepForm.FootMaterial, ref text);
						break;
					case CreateFootstepForm.ResultType.RT_Rect3:
						flag = this.CreateRectThirdSideFootstep(createFootstepForm.BottomLevel, createFootstepForm.Offset, createFootstepForm.IsInputPlatformWidth, createFootstepForm.PlatformWidth, createFootstepForm.StepCount, createFootstepForm.StepWidth, createFootstepForm.StepHeight, createFootstepForm.FootMaterial, ref text);
						break;
					case CreateFootstepForm.ResultType.RT_Bow:
						flag = this.CreateBowFootstep(createFootstepForm.BottomLevel, createFootstepForm.Offset, createFootstepForm.IsInputPlatformWidth, createFootstepForm.PlatformWidth, createFootstepForm.StepCount, createFootstepForm.StepWidth, createFootstepForm.StepHeight, createFootstepForm.FootMaterial, ref text);
						break;
					case CreateFootstepForm.ResultType.RT_Freedom:
						flag = this.CreateFreedomFootstep(createFootstepForm.BottomLevel, createFootstepForm.Offset, createFootstepForm.StepCount, createFootstepForm.StepWidth, createFootstepForm.StepHeight, createFootstepForm.FootMaterial, ref text);
						break;
					}
					if (!flag && text.CompareTo("") != 0)
					{
						MessageBox.Show(text, "盈建科提示");
					}
				}
				catch (Exception)
				{
					this.DeleteTempElements();
				}
				this.DeleteTempElements();
			}
		}

		private void DeleteTempElements()
		{
			Transaction transaction = null;
			transaction = new Transaction(this.m_Revit.Application.ActiveUIDocument.Document);
			transaction.Start("delete temp elements");
			foreach (ElementId elementId in this.m_TempElementIdList)
			{
				try
				{
					this.m_Revit.Application.ActiveUIDocument.Document.Delete(elementId);
				}
				catch (Exception)
				{
				}
			}
			transaction.Commit();
		}

		private bool CreateRectOneSideFootstep(Level bottomLevel, double offset, bool isInputPlatformWidth, double platformWidth, int stepCount, double stepWidth, double stepHeight, Material material, ref string message)
		{
			XYZ xyz = null;
			if (!this.SelelctPosision(ref xyz, "选择参照边起点"))
			{
				return false;
			}
			XYZ xyz2 = null;
			if (!this.SelelctPosisionSecond(ref xyz2, "选择参照边终点", xyz))
			{
				return false;
			}
			XYZ xyz3 = null;
			string statusPrompt = "选择宽度方向";
			if (!isInputPlatformWidth)
			{
				statusPrompt = "选择宽度参考点";
			}
			XYZ ptStart = xyz + (xyz2 - xyz) / 2.0;
			XYZ pt = (xyz2 - xyz).Normalize();
			XYZ direction = Geometry.RotateTo(pt, Math.PI*.5, XYZ.BasisZ);
			XYZ reverseDirection = Geometry.RotateTo(pt, -Math.PI*.5, XYZ.BasisZ);
			if (!this.SelelctDirection(ptStart, direction, reverseDirection, ref xyz3, statusPrompt))
			{
				return false;
			}
			XYZ xyz4 = Geometry.CalculateFootPoint(xyz, xyz2, xyz3);
			XYZ xyz5 = xyz3 - xyz4;
			if (isInputPlatformWidth)
			{
				xyz5 = xyz5.Normalize() * platformWidth;
			}
			List<LoopEdge> list = new List<LoopEdge>();
			LoopEdge item = new LoopEdge(xyz, xyz2, 0.0, null, LoopEdge.EdgeType.ET_Fixed);
			list.Add(item);
			LoopEdge item2 = new LoopEdge(xyz, xyz + xyz5, 0.0, null, LoopEdge.EdgeType.ET_Fixed);
			list.Add(item2);
			LoopEdge item3 = new LoopEdge(xyz2, xyz2 + xyz5, 0.0, null, LoopEdge.EdgeType.ET_Fixed);
			list.Add(item3);
			LoopEdge item4 = new LoopEdge(xyz + xyz5, xyz2 + xyz5, 0.0, null, LoopEdge.EdgeType.ET_Extensible);
			list.Add(item4);
			List<LoopEdge> serializedEdges = new List<LoopEdge>();
			if (!EdgeLoopAnalyser.SerializeEdgesToLoop(list, ref serializedEdges, ref message))
			{
				MessageBox.Show(message, "盈建科提示");
				return false;
			}
			return this.CreateExtrusionFootstep(serializedEdges, bottomLevel, offset, stepCount, stepWidth, stepHeight, material, ref message);
		}

		private bool CreateRectSecondSideFootstep(Level bottomLevel, double offset, bool isInputPlatformWidth, double platformWidth, int stepCount, double stepWidth, double stepHeight, Material material, ref string message)
		{
			XYZ xyz = null;
			if (!this.SelelctPosision(ref xyz, "选择参照边起点"))
			{
				return false;
			}
			XYZ xyz2 = null;
			if (!this.SelelctPosisionSecond(ref xyz2, "选择参照边终点", xyz))
			{
				return false;
			}
			XYZ xyz3 = null;
			string statusPrompt = "选择宽度方向";
			if (!isInputPlatformWidth)
			{
				statusPrompt = "选择宽度参考点";
			}
			XYZ xyz4 = xyz + (xyz2 - xyz) / 2.0;
			XYZ xyz5 = (xyz2 - xyz).Normalize();
			XYZ direction = Geometry.RotateTo(xyz5, Math.PI*.5, XYZ.BasisZ);
			XYZ reverseDirection = Geometry.RotateTo(xyz5, -Math.PI*.5, XYZ.BasisZ);
			if (!this.SelelctDirection(xyz4, direction, reverseDirection, ref xyz3, statusPrompt))
			{
				return false;
			}
			XYZ xyz6 = Geometry.CalculateFootPoint(xyz, xyz2, xyz3);
			XYZ xyz7 = xyz3 - xyz6;
			if (isInputPlatformWidth)
			{
				xyz7 = xyz7.Normalize() * platformWidth;
			}
			XYZ xyz8 = null;
			if (!this.SelelctDirection(xyz4 + xyz7 / 2.0, xyz5, -xyz5, ref xyz8, "选择另一侧台阶布置方向"))
			{
				return false;
			}
			LoopEdge.EdgeType edgeType = LoopEdge.EdgeType.ET_Extensible;
			LoopEdge.EdgeType edgeType2 = LoopEdge.EdgeType.ET_Fixed;
			if (Geometry.LessThan((xyz8 - xyz2).GetLength(), (xyz8 - xyz).GetLength()))
			{
				edgeType = LoopEdge.EdgeType.ET_Fixed;
				edgeType2 = LoopEdge.EdgeType.ET_Extensible;
			}
			List<LoopEdge> list = new List<LoopEdge>();
			LoopEdge item = new LoopEdge(xyz, xyz2, 0.0, null, LoopEdge.EdgeType.ET_Fixed);
			list.Add(item);
			LoopEdge item2 = new LoopEdge(xyz, xyz + xyz7, 0.0, null, edgeType);
			list.Add(item2);
			LoopEdge item3 = new LoopEdge(xyz2, xyz2 + xyz7, 0.0, null, edgeType2);
			list.Add(item3);
			LoopEdge item4 = new LoopEdge(xyz + xyz7, xyz2 + xyz7, 0.0, null, LoopEdge.EdgeType.ET_Extensible);
			list.Add(item4);
			List<LoopEdge> serializedEdges = new List<LoopEdge>();
			if (!EdgeLoopAnalyser.SerializeEdgesToLoop(list, ref serializedEdges, ref message))
			{
				MessageBox.Show(message, "盈建科提示");
				return false;
			}
			return this.CreateExtrusionFootstep(serializedEdges, bottomLevel, offset, stepCount, stepWidth, stepHeight, material, ref message);
		}

		private bool CreateRectThirdSideFootstep(Level bottomLevel, double offset, bool isInputPlatformWidth, double platformWidth, int stepCount, double stepWidth, double stepHeight, Material material, ref string message)
		{
			XYZ xyz = null;
			if (!this.SelelctPosision(ref xyz, "选择参照边起点"))
			{
				return false;
			}
			XYZ xyz2 = null;
			if (!this.SelelctPosisionSecond(ref xyz2, "选择参照边终点", xyz))
			{
				return false;
			}
			XYZ xyz3 = null;
			string statusPrompt = "选择宽度方向";
			if (!isInputPlatformWidth)
			{
				statusPrompt = "选择宽度参考点";
			}
			XYZ ptStart = xyz + (xyz2 - xyz) / 2.0;
			XYZ pt = (xyz2 - xyz).Normalize();
			XYZ direction = Geometry.RotateTo(pt, Math.PI*.5, XYZ.BasisZ);
			XYZ reverseDirection = Geometry.RotateTo(pt, -Math.PI*.5, XYZ.BasisZ);
			if (!this.SelelctDirection(ptStart, direction, reverseDirection, ref xyz3, statusPrompt))
			{
				return false;
			}
			XYZ xyz4 = Geometry.CalculateFootPoint(xyz, xyz2, xyz3);
			XYZ xyz5 = xyz3 - xyz4;
			if (isInputPlatformWidth)
			{
				xyz5 = xyz5.Normalize() * platformWidth;
			}
			List<LoopEdge> list = new List<LoopEdge>();
			LoopEdge item = new LoopEdge(xyz, xyz2, 0.0, null, LoopEdge.EdgeType.ET_Fixed);
			list.Add(item);
			LoopEdge item2 = new LoopEdge(xyz, xyz + xyz5, 0.0, null, LoopEdge.EdgeType.ET_Extensible);
			list.Add(item2);
			LoopEdge item3 = new LoopEdge(xyz2, xyz2 + xyz5, 0.0, null, LoopEdge.EdgeType.ET_Extensible);
			list.Add(item3);
			LoopEdge item4 = new LoopEdge(xyz + xyz5, xyz2 + xyz5, 0.0, null, LoopEdge.EdgeType.ET_Extensible);
			list.Add(item4);
			List<LoopEdge> serializedEdges = new List<LoopEdge>();
			if (!EdgeLoopAnalyser.SerializeEdgesToLoop(list, ref serializedEdges, ref message))
			{
				MessageBox.Show(message, "盈建科提示");
				return false;
			}
			return this.CreateExtrusionFootstep(serializedEdges, bottomLevel, offset, stepCount, stepWidth, stepHeight, material, ref message);
		}

		private bool CreateBowFootstep(Level bottomLevel, double offset, bool isInputPlatformWidth, double platformWidth, int stepCount, double stepWidth, double stepHeight, Material material, ref string message)
		{
			XYZ xyz = null;
			if (!this.SelelctPosision(ref xyz, "选择参照边起点"))
			{
				return false;
			}
			XYZ xyz2 = null;
			if (!this.SelelctPosisionSecond(ref xyz2, "选择参照边终点", xyz))
			{
				return false;
			}
			XYZ xyz3 = null;
			string statusPrompt = "选择宽度方向";
			if (!isInputPlatformWidth)
			{
				statusPrompt = "选择宽度参考点";
			}
			XYZ xyz4 = xyz + (xyz2 - xyz) / 2.0;
			XYZ pt = (xyz2 - xyz).Normalize();
			XYZ direction = Geometry.RotateTo(pt, Math.PI*.5, XYZ.BasisZ);
			XYZ reverseDirection = Geometry.RotateTo(pt, -Math.PI*.5, XYZ.BasisZ);
			if (!this.SelelctDirection(xyz4, direction, reverseDirection, ref xyz3, statusPrompt))
			{
				return false;
			}
			XYZ xyz5 = Geometry.CalculateFootPoint(xyz, xyz2, xyz3);
			XYZ xyz6 = xyz3 - xyz5;
			if (isInputPlatformWidth)
			{
				xyz6 = xyz6.Normalize() * platformWidth;
			}
			List<LoopEdge> list = new List<LoopEdge>();
			LoopEdge item = new LoopEdge(xyz, xyz2, 0.0, null, LoopEdge.EdgeType.ET_Fixed);
			list.Add(item);
			double num = xyz6.GetLength() / (xyz4 - xyz).GetLength();
			XYZ pt2 = xyz4 + xyz6;
			if (Geometry.PointAtLineLeft(pt2, xyz, xyz2))
			{
				num = -num;
			}
			LoopEdge item2 = new LoopEdge(xyz, xyz2, num, null, LoopEdge.EdgeType.ET_Extensible);
			list.Add(item2);
			List<LoopEdge> serializedEdges = new List<LoopEdge>();
			if (!EdgeLoopAnalyser.SerializeEdgesToLoop(list, ref serializedEdges, ref message))
			{
				MessageBox.Show(message, "盈建科提示");
				return false;
			}
			return this.CreateExtrusionFootstep(serializedEdges, bottomLevel, offset, stepCount, stepWidth, stepHeight, material, ref message);
		}

		private bool CreateFreedomFootstep(Level bottomLevel, double offset, int stepCount, double stepWidth, double stepHeight, Material material, ref string message)
		{
			List<LoopEdge> edges = this.SelectRefrenceCurves();
			List<LoopEdge> serializedEdges = new List<LoopEdge>();
			return EdgeLoopAnalyser.SerializeEdgesToLoop(edges, ref serializedEdges, ref message) && this.SelectFixedEdges(ref serializedEdges) && this.CreateExtrusionFootstep(serializedEdges, bottomLevel, offset, stepCount, stepWidth, stepHeight, material, ref message);
		}

		private bool CreateExtrusionFootstep(List<LoopEdge> serializedEdges, Level bottomLevel, double offset, int stepCount, double stepWidth, double stepHeight, Material material, ref string message)
		{
			XYZ startPoint = serializedEdges[0].StartPoint;
			foreach (LoopEdge loopEdge in serializedEdges)
			{
				loopEdge.Move(-startPoint);
			}
			EdgeLoopOffsetEditor edgeLoopOffsetEditor = new EdgeLoopOffsetEditor(this.m_Revit.Application.Application);
			List<List<LoopEdge>> list = new List<List<LoopEdge>>();
			int i = 0;
			while (i < stepCount)
			{
				List<LoopEdge> list2 = null;
				if (i == 0)
				{
					list2 = serializedEdges;
					goto IL_C7;
				}
				list2 = edgeLoopOffsetEditor.OffsetEdgeLoop(serializedEdges, -stepWidth * (double)i);
				if (list2.Count != 0)
				{
					foreach (LoopEdge loopEdge2 in list2)
					{
						loopEdge2.SetElevation(0.0);
					}
					goto IL_C7;
				}
				IL_CF:
				i++;
				continue;
				IL_C7:
				list.Add(list2);
				goto IL_CF;
			}
			MassingFamilyDesigner massingFamilyDesigner = new MassingFamilyDesigner(this.m_Revit, material.Id);
			string familiyPath = "";
			string familyName = "";
			string symbolName = "";
			Result result = massingFamilyDesigner.DesignExtrusionFootstep(list, stepHeight, ref familiyPath, ref familyName, ref symbolName, ref message);
			return result == Result.Succeeded && this.LoadFamiliesFile(familiyPath, ref message) && this.CreateElement(startPoint, bottomLevel, offset, familyName, symbolName, material, ref message);
		}

		private bool LoadFamiliesFile(string familiyPath, ref string message)
		{
			Transaction transaction = new Transaction(this.m_Revit.Application.ActiveUIDocument.Document);
			transaction.Start("load families");
			this.m_Revit.Application.ActiveUIDocument.Document.LoadFamily(familiyPath);
			transaction.Commit();
			return true;
		}

		private bool CreateElement(XYZ locationPoint, Level bottomLevel, double offset, string familyName, string symbolName, Material material, ref string message)
		{
			Transaction transaction = null;
			transaction = new Transaction(this.m_Revit.Application.ActiveUIDocument.Document);
			transaction.Start("create element");
			FamilySymbol familySymbolByName = this.GetFamilySymbolByName(familyName, symbolName);
			familySymbolByName.GetParameter("材质").Set(material.Id);
			FamilyInstance familyInstance = this.CreateOneFootstep(locationPoint, bottomLevel, offset, familySymbolByName, false);
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document);
			ElementIntersectsElementFilter elementIntersectsElementFilter = new ElementIntersectsElementFilter(familyInstance, false);
			filteredElementCollector.WherePasses(elementIntersectsElementFilter);
			filteredElementCollector.Excluding(new List<ElementId>
			{
				familyInstance.Id
			});
			foreach (Element element in filteredElementCollector)
			{
				if (element.Category.Id.IntegerValue == -2000151)
				{
					TaskDialog.Show("Error", "插入点已有其他不允许相交的设备存在！");
					transaction.RollBack();
					return false;
				}
			}
			transaction.Commit();
			return true;
		}

		private FamilySymbol GetFamilySymbolByName(string familyName, string symbolName)
		{
			FamilySymbol result = null;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document);
			IList<Element> list = filteredElementCollector.OfClass(typeof(Family)).ToElements();
			foreach (Element element in list)
			{
				Family family = element as Family;
				if (family != null)
				{
					Category familyCategory = RevitVersionFuncs.GetFamilyCategory(family);
					if (familyCategory != null && familyName.ToUpper().CompareTo(family.Name.ToUpper()) == 0)
					{
						List<FamilySymbol> symbols = family.GetSymbols();
						foreach (FamilySymbol familySymbol in symbols)
						{
							if (symbolName.ToUpper().CompareTo(familySymbol.Name.ToUpper()) == 0)
							{
								return familySymbol;
							}
						}
					}
				}
			}
			return result;
		}

		private FamilyInstance CreateOneFootstep(XYZ locationPoint, Level bottomLevel, double offset, FamilySymbol footstepType, bool isStructure)
		{
			UnifiedModified.ActivateFamilySymbol(footstepType);
			FamilyInstance familyInstance = this.m_Revit.Application.ActiveUIDocument.Document.Create.NewFamilyInstance(locationPoint, footstepType, bottomLevel, 0);
			if (familyInstance != null)
			{
				Parameter parameter = familyInstance.get_Parameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
				if (parameter != null)
				{
					parameter.Set(offset);
				}
			}
			return familyInstance;
		}

		private List<LoopEdge> SelectRefrenceCurves()
		{
			List<LoopEdge> list = new List<LoopEdge>();
			IList<Reference> list2 = null;
			Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
			try
			{
				list2 = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new CurveFilter(), "选择连续模型/详图线");
			}
			catch (Exception)
			{
				return list;
			}
			if (list2.Count < 1)
			{
				return list;
			}
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			foreach (Reference reference in list2)
			{
				Element element = document.GetElement(reference);
				DetailLine detailLine = element as DetailLine;
				if (detailLine != null)
				{
					Line curve = detailLine.GeometryCurve as Line;
					LoopEdge item = new LoopEdge(curve.GetEndPoint(0), curve.GetEndPoint(1), 0.0, element.Id, LoopEdge.EdgeType.ET_Extensible);
					list.Add(item);
				}
				else
				{
					DetailArc detailArc = element as DetailArc;
					if (detailArc != null)
					{
						Arc arc = detailArc.GeometryCurve as Arc;
						bool isAnticlockwise = arc.Normal.IsAlmostEqualTo(XYZ.BasisZ);
						double bulge = Geometry.GetBulge(arc.GetEndPoint(0), arc.GetEndPoint(1), arc.Center, isAnticlockwise);
						LoopEdge item2 = new LoopEdge(arc.GetEndPoint(0), arc.GetEndPoint(1), bulge, element.Id, LoopEdge.EdgeType.ET_Extensible);
						list.Add(item2);
					}
					else
					{
						ModelLine modelLine = element as ModelLine;
						if (modelLine != null)
						{
							Line curve2 = modelLine.GeometryCurve as Line;
							LoopEdge item3 = new LoopEdge(curve2.GetEndPoint(0), curve2.GetEndPoint(1), 0.0, element.Id, LoopEdge.EdgeType.ET_Extensible);
							list.Add(item3);
						}
						else
						{
							ModelArc modelArc = element as ModelArc;
							if (modelArc != null)
							{
								Arc arc2 = modelArc.GeometryCurve as Arc;
								bool isAnticlockwise2 = arc2.Normal.IsAlmostEqualTo(XYZ.BasisZ);
								double bulge2 = Geometry.GetBulge(arc2.GetEndPoint(0), arc2.GetEndPoint(1), arc2.Center, isAnticlockwise2);
								LoopEdge item4 = new LoopEdge(arc2.GetEndPoint(0), arc2.GetEndPoint(1), bulge2, element.Id, LoopEdge.EdgeType.ET_Extensible);
								list.Add(item4);
							}
						}
					}
				}
			}
			return list;
		}

		private bool SelectFixedEdges(ref List<LoopEdge> edgeLoop)
		{
			IList<Reference> list = null;
			Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
			try
			{
				list = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, new CurveFilter(), "选择固定边");
			}
			catch (Exception)
			{
				return false;
			}
			if (list.Count < 1)
			{
				return true;
			}
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			foreach (Reference reference in list)
			{
				Element element = document.GetElement(reference);
				int nId = element.Id.IntegerValue;
				int num = edgeLoop.FindIndex((LoopEdge s) => s.CurveId.IntegerValue == nId);
				if (num != -1)
				{
					edgeLoop[num].TheEdgeType = LoopEdge.EdgeType.ET_Fixed;
				}
			}
			return true;
		}

		private bool SelelctPosision(ref XYZ pt, string statusPrompt)
		{
			Transaction transaction = null;
			transaction = new Transaction(this.m_Revit.Application.ActiveUIDocument.Document);
			transaction.Start("selelct posision");
			try
			{
				pt = this.m_Revit.Application.ActiveUIDocument.Selection.PickPoint(statusPrompt);
				double num = AssistFunc.mmToFeet(200.0);
				CurveArray curveArray = new CurveArray();
				Line line = YJKLineEx.YJKGetBound(new XYZ(pt.X - num, pt.Y + num, 0.0), new XYZ(pt.X + num, pt.Y - num, 0.0));
				curveArray.Append(line);
				Line line2 = YJKLineEx.YJKGetBound(new XYZ(pt.X + num, pt.Y + num, 0.0), new XYZ(pt.X - num, pt.Y - num, 0.0));
				curveArray.Append(line2);
                Autodesk.Revit.DB.View activeView = this.m_Revit.Application.ActiveUIDocument.ActiveView;
				DetailCurveArray detailCurveArray = this.m_Revit.Application.ActiveUIDocument.Document.Create.NewDetailCurveArray(activeView, curveArray);
				foreach (object obj in detailCurveArray)
				{
					DetailCurve detailCurve = (DetailCurve)obj;
					this.m_TempElementIdList.Add(detailCurve.Id);
				}
				transaction.Commit();
			}
			catch (Exception)
			{
				transaction.RollBack();
				return false;
			}
			return true;
		}

		private bool SelelctPosisionSecond(ref XYZ pt, string statusPrompt, XYZ ptLast)
		{
			XLineJig xlineJig = new XLineJig(this.m_uiDoc);
			xlineJig.StartPoint = ptLast;
			Transaction transaction = null;
			transaction = new Transaction(this.m_Revit.Application.ActiveUIDocument.Document);
			transaction.Start("selelct posision");
			try
			{
				pt = xlineJig.PickEndPoint(ptLast, statusPrompt);
				double num = AssistFunc.mmToFeet(200.0);
				CurveArray curveArray = new CurveArray();
				Line line = YJKLineEx.YJKGetBound(new XYZ(pt.X - num, pt.Y + num, 0.0), new XYZ(pt.X + num, pt.Y - num, 0.0));
				curveArray.Append(line);
				Line line2 = YJKLineEx.YJKGetBound(new XYZ(pt.X + num, pt.Y + num, 0.0), new XYZ(pt.X - num, pt.Y - num, 0.0));
				curveArray.Append(line2);
				Line line3 = YJKLineEx.YJKGetBound(ptLast, pt);
				curveArray.Append(line3);
                Autodesk.Revit.DB.View activeView = this.m_Revit.Application.ActiveUIDocument.ActiveView;
				DetailCurveArray detailCurveArray = this.m_Revit.Application.ActiveUIDocument.Document.Create.NewDetailCurveArray(activeView, curveArray);
				foreach (object obj in detailCurveArray)
				{
					DetailCurve detailCurve = (DetailCurve)obj;
					this.m_TempElementIdList.Add(detailCurve.Id);
				}
				xlineJig.Dispose();
				transaction.Commit();
			}
			catch (Exception)
			{
				xlineJig.Dispose();
				transaction.RollBack();
				return false;
			}
			return true;
		}

		private bool SelelctDirection(XYZ ptStart, XYZ direction, XYZ reverseDirection, ref XYZ pt, string statusPrompt)
		{
			XLineJig xlineJig = new XLineJig(this.m_uiDoc);
			xlineJig.StartPoint = ptStart;
			Transaction transaction = null;
			transaction = new Transaction(this.m_Revit.Application.ActiveUIDocument.Document);
			transaction.Start("selelct direction");
			List<ElementId> list = new List<ElementId>();
			try
			{
				list.AddRange(this.DrawArrows(ptStart, direction));
				list.AddRange(this.DrawArrows(ptStart, reverseDirection));
				pt = xlineJig.PickEndPoint(ptStart, statusPrompt);
				foreach (ElementId elementId in list)
				{
					this.m_Revit.Application.ActiveUIDocument.Document.Delete(elementId);
				}
				xlineJig.Dispose();
				transaction.Commit();
			}
			catch (Exception)
			{
				xlineJig.Dispose();
				transaction.RollBack();
				return false;
			}
			return true;
		}

		private List<ElementId> DrawArrows(XYZ ptStart, XYZ direction)
		{
			double num = AssistFunc.mmToFeet(400.0);
			double num2 = num * 3.0;
			XYZ xyz = Geometry.RotateTo(-direction, 0.52359877559829882, XYZ.BasisZ).Normalize() * num;
			XYZ xyz2 = Geometry.RotateTo(-direction, -0.52359877559829882, XYZ.BasisZ).Normalize() * num;
			XYZ xyz3 = ptStart + direction * num2;
			CurveArray curveArray = new CurveArray();
			Line line = YJKLineEx.YJKGetBound(ptStart + direction / 3.0, xyz3);
			curveArray.Append(line);
			Line line2 = YJKLineEx.YJKGetBound(xyz3, xyz3 + xyz2);
			curveArray.Append(line2);
			Line line3 = YJKLineEx.YJKGetBound(xyz3, xyz3 + xyz);
			curveArray.Append(line3);
            Autodesk.Revit.DB.View activeView = this.m_Revit.Application.ActiveUIDocument.ActiveView;
			DetailCurveArray detailCurveArray = this.m_Revit.Application.ActiveUIDocument.Document.Create.NewDetailCurveArray(activeView, curveArray);
			List<ElementId> list = new List<ElementId>();
			foreach (object obj in detailCurveArray)
			{
				DetailCurve detailCurve = (DetailCurve)obj;
				list.Add(detailCurve.Id);
			}
			Common.UpdateModel(this.m_Revit.Application.ActiveUIDocument, false);
			return list;
		}

		private ExternalCommandData m_Revit;

		private List<ElementId> m_LevelIds = new List<ElementId>();

		private List<ElementId> m_TempElementIdList = new List<ElementId>();

		private UIDocument m_uiDoc;

		private DrawJigCurve m_drawJigCurve;
	}
}
